import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/components/login/login'
import HomePage from '@/components/home/homePage'
import Layout from '@/components/layout/layout'
import StoreRankingList from '@/components/storeManage/storeRankingList'

import { getStorageToken } from '@/proConfig/localUtils'

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/login',
      name: 'login',
      component: Login
    }, {
      path: '/',
      name: 'layout',
      component: Layout,
      // 默认页面
      redirect: '/homePage',
      // 余下所有页面都配置在children中(vue的sb逻辑)
      children: [{
        path: 'homePage',
        component: HomePage
      }, {
        path: 'storeRankingList',
        component: StoreRankingList
      }]
    },
  ]
})

/**
 * 登录拦截
 *
 * 参数1 : to 目标路由对象
 * 参数2 : from 来源路由对象
 * 参数3 : next() 下一步
 */
router.beforeEach((to, from, next) => {
  // 判断是否登录页面
  if (to.path === '/login') {
    next()
  } else {
    // 获取token
    let token = getStorageToken()
    if (token) {
      next()
    } else {
      next('/login')
    }
  }
})
export default router
